#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> tr;

int main(int argc, char const *argv[])
{
    int n; scanf("%d", &n);
    for (int kk=0; kk<n; ++kk)
    {
        int op, x; scanf("%d%d", &op, &x);
        switch (op)
        {
        case 1:
            tr.insert(make_pair(x, kk));
            break;
        case 2:
            tr.erase(tr.lower_bound(make_pair(x, 0)));
            break;
        case 3:
            printf("%d\n", (int)tr.order_of_key(*tr.lower_bound(make_pair(x, 0))) + 1);
            break;
        case 4:
            printf("%d\n", tr.find_by_order(x-1)->first);
            break;
        case 5:
            printf("%d\n", (--tr.lower_bound(make_pair(x, 0)))->first);
            break;
        default:
            printf("%d\n", tr.upper_bound(make_pair(x, n))->first);
            break;
        }
    }
    return 0;
}